package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.common.constant.SystemConstants;
import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.common.util.StringUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysRole;
import com.zzl.alligator.management.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author zhouziliang
 * @since 2020/7/2
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    /**
     * 查找角色列表
     *
     * @param sysRole
     * @return
     */
    @GetMapping
    public JsonResult findSysRoleList(SysRole sysRole) {
        startPage();
        List<SysRole> roleList = roleService.findRoleList(sysRole);
        return JsonResult.success(getDataTable(roleList));
    }

    /**
     * 根据角色Id查找角色信息
     *
     * @param roleId 角色Id
     * @return 角色
     */
    @GetMapping("/{roleId}")
    public JsonResult findSysRoleById(@PathVariable Long roleId) {
        SysRole role = roleService.findRoleById(roleId);
        return JsonResult.success(role);
    }

    /**
     * 插入一条角色信息
     *
     * @param sysRole 角色信息
     * @return 结果
     */
    @PostMapping
    @Log(businessType = BusinessTypeEnum.INSERT, title = "新增角色信息")
    public JsonResult insertSysRole(@Validated @RequestBody SysRole sysRole) {
        if (SystemConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(sysRole))) {
            return JsonResult.error("新增角色失败, 角色名 " + sysRole.getRoleName() + " 已存在");
        }
        if (SystemConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(sysRole))) {
            return JsonResult.error("新增角色失败，角色权限 " + sysRole.getRoleKey() + " 已存在");
        }
        int i = roleService.insertRole(sysRole);
        return i > 0 ? JsonResult.success(i) : JsonResult.error("新增失败");
    }

    /**
     * 更新角色
     *
     * @param sysRole 角色信息
     * @return 更新结果
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "更新角色信息", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult updateSysRole(@Validated @RequestBody SysRole sysRole) {
        roleService.checkRoleAllowed(sysRole);
        if (SystemConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(sysRole))) {
            return JsonResult.error("修改用户失败，角色权限" + sysRole.getRoleKey() + "已存在");
        }
        if (SystemConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(sysRole))) {
            return JsonResult.error("修改用户失败，角色名" + sysRole.getRoleName() + "已存在");
        }
        int i = roleService.updateRole(sysRole);
        return i > 0 ? JsonResult.success(i) : JsonResult.error("更新失败");
    }

    /**
     * 更新角色
     *
     * @param sysRole 角色信息
     * @return 更新结果
     */
    @PutMapping("/changeStatus")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "修改角色状态", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult changeStatus(@RequestBody SysRole sysRole) {
        roleService.checkRoleAllowed(sysRole);
        int i = roleService.updateRoleStatus(sysRole);
        return i > 0 ? JsonResult.success(i) : JsonResult.error("更新失败");
    }

    /**
     * 删除角色
     *
     * @param roleIds 角色ID
     * @return 删除结果
     */
    @DeleteMapping("/{roleIds}")
    @Log(title = "删除角色信息", businessType = BusinessTypeEnum.DELETE)
    public JsonResult deleteRole(@PathVariable("roleIds") String roleIds) {
        if(StringUtils.isBlank(roleIds)) {
            return JsonResult.success();
        }
        String[] roleIdArr = roleIds.split(",");
        List<Long> idList = Arrays.stream(roleIdArr).map(Long::valueOf).collect(Collectors.toList());
        return toAjax(roleService.deleteRoleByIds(idList.toArray(new Long[0])));
    }

    /**
     * 导出数据
     *
     * @param role 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出角色信息", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(SysRole role) {
        List<SysRole> list = roleService.findRoleList(role);
        ExcelUtils<SysRole> util = new ExcelUtils<>(SysRole.class);
        return util.exportExcel(list, "角色数据");
    }

}
